<% layout('/layouts/default.html', {title: '列检作业记录管理', libs: ['dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header">
			<div class="box-title">
				<i class="fa icon-notebook"></i> ${text('列检作业记录管理')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btnSearch" title="${text('查询')}"><i class="fa fa-filter"></i> ${text('查询')}</a>
				<!-- <% if(hasPermi('tbl:tblXdLjzyjl:edit')){ %>
					<a href="${ctx}/tbl/tblXdLjzyjl/form" class="btn btn-default btnTool" title="${text('新增列检作业记录')}"><i class="fa fa-plus"></i> ${text('新增')}</a>
				<% } %> -->
				<a href="#" class="btn btn-default" id="btnSetting" title="${text('设置')}"><i class="fa fa-navicon"></i></a>
			</div>
		</div>
		<div class="box-body">
			<#form:form id="searchForm" model="${tblXdLjzyjl}" action="${ctx}/tbl/tblXdLjzyjl/listData" method="post" class="form-inline"
					data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
			<div class="form-group">
				<label class="control-label">${text('日期')}：</label>
				<div class="control-inline">
					<#form:input path="stationTime" readonly="true" maxlength="20" class="form-control laydate"
					dataFormat="date" data-type="date" data-format="yyyy-MM-dd" />
				</div>
			</div>
			<div class="form-group">
					<label class="control-label">${text('班别')}：</label>
					<div class="control-inline width-120">
						<#form:select path="shift" dictType="shift_type" blankOption="flase" class="form-control" id="shift"/>
					</div>
				</div>

			<div class="form-group">
				<label class="control-label">${text('车站')}：</label>
				<div class="control-inline width-120">
					<#form:select path="stationCode" maxlength="2" items="${lineList}" itemLabel="stationName" itemValue="stationCode" class="form-control"/>
				</div>
			</div>

				<div class="form-group">
					<button type="submit" class="btn btn-primary btn-sm">${text('查询')}</button>
					<a class="btn btn-primary btn-sm" onclick="confirm()">  确认  </a>
					<a class="btn btn-primary btn-sm" onclick="cancel()">  取消  </a>
					<a class="btn btn-primary btn-sm" onclick="removeSc()">  结束  </a>
					<button type="reset" class="btn btn-default btn-sm">${text('重置')}</button>
				</div>
				
				<div class="form-group">
					<div style="display: flex;align-items: center;">
						<div style="display: flex;align-items: center;" id="czp01">
							<img src="${ctxStatic}/img/czp2.png" id="czp1" style="width:100px;margin: 0 30px;">
							<label><font size="3"><b>车站</b></font></label>
						</div>
<!--						<img src="${ctxStatic}/img/czp2.png" id="czp1" style="width:100px;margin: 0 30px;">-->
<!--						<label><font size="3"><b>车站</b></font></label>-->
						<img src="${ctxStatic}/img/sx.jpg" style="margin: 0 10px;">
<!--						<label><font size="3"><b>列检</b></font></label>-->
<!--						<img src="${ctxStatic}/img/czp2.png" id="czp2" class="invisible" style="width:100px;margin: 0 30px;">-->
						<div style="display: flex;align-items: center;" id="czp02" class="invisible">
							<label><font size="3"><b>列检</b></font></label>
							<img src="${ctxStatic}/img/czp2.png" id="czp2" style="width:100px;margin: 0 30px;">
						</div>
					</div>
				</div>
			</#form:form>
			<table id="dataGrid"></table>
			<div id="dataGridPage"></div>
			<div id="comnfirmBox" data-url="${ctx}/tbl/tblXdLjzyjl/form" style="display: none">
			<div id="finishBox" data-url="${ctx}/tbl/tblXdLjzyjl/form2" style="display: none">
		</div>
	</div>
</div>
<% } %>
<script>
	$(function(){
		// 定义一个函数来更新班次和日期
		function updateShiftAndTime() {
			var $shift = $("#shift");
			var date_now = new Date();
			var hours = date_now.getHours();
			var year = date_now.getFullYear();  // 获取年
			var month = date_now.getMonth() + 1;  // 获取月
			var day = date_now.getDate();  // 获取日
			var $stationTime = $("#stationTime");
			console.log(hours);
			// 设置班次和日期
			// 判断是否是夜班
			if (hours >= 19) {
				// 夜班 (19:00到0:00)，日期保持不变
				$shift.val('1').trigger('change'); // 夜班
				date_now.setDate(date_now.getDate() + 1);// 日期加一天
				day = date_now.getDate();  // 更新日
				month = date_now.getMonth() + 1;  // 更新月
				year = date_now.getFullYear();  // 更新年
			} else if (hours < 8) {
				// 夜班 (0:00到8:00)，日期减去一天
				$shift.val('1').trigger('change'); // 夜班
				date_now.setDate(date_now.getDate()); // 日期
				year = date_now.getFullYear();  // 更新年
				month = date_now.getMonth() + 1;  // 更新月
				day = date_now.getDate();  // 更新日
			} else {
				// 白班 (8:00到19:00)
				$shift.val('2').trigger('change'); // 白班
			}
			// 设置日期，补零处理
			$stationTime.val(year + "-" + (month < 10 ? '0' + month : month) + "-" + (day < 10 ? '0' + day : day));
		}

		// 页面加载时更新一次
		updateShiftAndTime();

		// 每隔一分钟更新一次班次和日期
		// setInterval(updateShiftAndTime, 60000);
		console.log(tblXdLjzyjl.userId)
	});

// 初始化DataGrid对象
$('#dataGrid').dataGrid({
	searchForm: $("#searchForm"),
	multiselect:true,
	rownumWidth:40,
	shrinkToFit:false,
	columnModel: [
		{header:'${text("是否确认")}', name:'status', index:'a.status', width:150, align:"center", frozen:true, formatter: function(val, obj, row, act){
			return js.getDictLabel(${@DictUtils.getDictListJson('status')}, val, '${text("未知")}', true);
		}},
		// {header:'${text("车数")}', name:'wagonNum', index:'a.wagon_num', width:150, align:"center",formatter:'number'},
		{header:'${text("班别")}', name:'shift', index:'a.shift', width:150, align:"center", formatter: function(val, obj, row, act){
			return js.getDictLabel(${@DictUtils.getDictListJson('shift_type')}, val, '${text("未知")}', true);
		}},
		{header:'${text("车数")}', name:'wagonNum', index:'a.wagonNum', width:150, align:"center"},
		{header:'${text("线路")}', name:'line', index:'a.line', width:150, align:"center"},
		{header:'${text("车站发起人")}', name:'stationUsername', index:'a.station_username', width:150, align:"center"},
		{header:'${text("车站发起时间")}', name:'stationTime', index:'a.station_time', width:180, align:"center"},
		{header:'${text("确认人")}', name:'confirmUsername', index:'a.confirm_username', width:150, align:"center"},
		{header:'${text("确认时间")}', name:'confirmTime', index:'a.confirm_time', width:180, align:"center"},
		{header:'${text("取消人")}', name:'cancelUsername', index:'a.cancel_username', width:150, align:"center"},
		{header:'${text("取消时间")}', name:'cancelTime', index:'a.cancel_time', width:180, align:"center"},
		{header:'${text("结束人")}', name:'finishUsername', index:'a.finish_username', width:150, align:"center"},
		{header:'${text("结束时间")}', name:'finishTime', index:'a.finish_time', width:180, align:"center"},
		//{header:'${text("检车人")}', name:'jcrCode', index:'a.jcr_code', width:150, align:"center"},
		/* {header:'${text("操作")}', name:'actions', width:120, formatter: function(val, obj, row, act){
			var actions = [];
			//<% if(hasPermi('tbl:tblXdLjzyjl:edit')){ %>
				actions.push('<a href="${ctx}/tbl/tblXdLjzyjl/form?recId='+row.recId+'" class="btnList" title="${text("编辑列检作业记录")}"><i class="fa fa-pencil"></i></a>&nbsp;');
				actions.push('<a href="${ctx}/tbl/tblXdLjzyjl/delete?recId='+row.recId+'" class="btnList" title="${text("删除列检作业记录")}" data-confirm="${text("确认要删除该列检作业记录吗？")}"><i class="fa fa-trash-o"></i></a>&nbsp;');
			//<% } %>
			return actions.join('');
		}} */
	],
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		const array = JSON.parse(sessionStorage.getItem("列检作业记录管理"))
		if(array && array.length > 0){
			for (const item of array) {
				$('#dataGrid').dataGrid('setSelectRow', item, false);
			}
		}
	},
	//数据加载前执行方法
	ajaxLoad: function(data){
		/* var stationTime = $("#stationTime").val();
		if(stationTime == null || stationTime ==""){
			js.showMessage('查询日期不能为空！', '', 'warning', 4000);
			return;
		} */
	},
	onSelectRow: function(id, isSelect, event){
		var data = $('#dataGrid').dataGrid('getRowData', id);
		var status = data.status;
		if(status == "已确认"){
			// $('#czp1').addClass('invisible').css('width','30px');
			// $('#czp2').removeClass('invisible').css('width','110px');
			$('#czp01').addClass('invisible');
			$('#czp02').removeClass('invisible')
			$('#czp2').css('width','110px')
		}else{
			// $('#czp2').addClass('invisible').css('width','30px');
			// $('#czp1').removeClass('invisible').css('width','110px');
			$('#czp02').addClass('invisible');
			$('#czp01').removeClass('invisible');
			$('#czp1').css('width','110px');
		}

		const array = $('#dataGrid').dataGrid('getSelectRows')
		sessionStorage.setItem("列检作业记录管理", JSON.stringify(array))
	},
}).dataGrid('setLabel', 'rn', '序号');

$('button[type="reset"]').on('click', function() {
	//移除暂存
	sessionStorage.removeItem("列检作业记录管理")
	$('#dataGrid').dataGrid('resetSelection');
})
 
//确认
function confirm(){
	var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
	if(ids.length>0){
		if(ids.length!=1){
			js.showMessage('不允许同时多条记录确认。', '', 'warning', 4000);
			return;
		}else{
			var rowData = $('#dataGrid').dataGrid('getRowData', ids[0]);
			if(rowData.status != "未确认"){
				js.showMessage('列检作业状态不是未确认，无法进行确认操作。', '', 'warning', 4000);
				return;
			}
			
			$('#comnfirmBox').show();
			js.layer.open({
		       type: 2,
		       title:['列检确认','background-color:#e2e1e1;'],
		       align:'center',
		       area: ['842px', '380px'], //宽高
		       content: $('#comnfirmBox').attr('data-url')+"?recId="+ids[0],
		       shade: 0.5 ,
		       resize:false,//弹窗不可拖拽 
		       end : function(){
		           $('#comnfirmBox').hide();
		           $('#dataGrid').trigger("reloadGrid");
		       }
		    });
		}
	}else{
		 js.showMessage('请选择一条记录。', '', 'warning', 4000);
		 return;
	}
}

//结束
function removeSc(){
	var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
	if(ids.length>0){
		if(ids.length!=1){
			js.showMessage('不允许同时多条记录结束。', '', 'warning', 4000);
			return;
		}else{
			var rowData = $('#dataGrid').dataGrid('getRowData', ids[0]);
			if(rowData.status != "已确认"){
				js.showMessage('列检作业状态不是已确认，无法结束。', '', 'warning', 4000);
				return;
			}
			$('#finishBox').show();
			js.layer.open({
		       type: 2,
		       title:['列检结束','background-color:#e2e1e1;'],
		       align:'center',
		       area: ['842px', '330px'], //宽高
		       content: $('#finishBox').attr('data-url')+"?recId="+ids[0],
		       shade: 0.5 ,
		       resize:false,//弹窗不可拖拽 
		       end : function(){
		           $('#finishBox').hide();
		           $('#dataGrid').trigger("reloadGrid");
		       }
		    });
		}
	}else{
		 js.showMessage('请选择一条记录。', '', 'warning', 4000);
		 return;
	}
}

//取消
function cancel(){
	var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
	if(ids.length>0){
		if(ids.length!=1){
			js.showMessage('不允许同时多条记录结束。', '', 'warning', 4000);
			return;
		}else{
			var msg = "确认进行【取消】操作？";
			js.confirm(msg,function(data){
				var rowData = $('#dataGrid').dataGrid('getRowData', ids[0]);
				console.log(rowData.status);
				if(rowData.status == "已确认"){
					js.showMessage('列检作业状态已确认，无法取消。', '', 'warning', 4000);
					return;
				}
				if(rowData.status == "已取消"){
					js.showMessage('列检作业状态已取消，不要重复操作。', '', 'warning', 4000);
					return;
				}	if(rowData.status == "已结束"){
					js.showMessage('列检作业状态已结束，无法取消。', '', 'warning', 4000);
					return;
				}
			    $.post("${ctx}/tbl/tblXdLjzyjl/cancel",{'recId':ids[0]},function(e){
			    	if(e.result=true){
			    		js.showMessage(e.message, '', 'success', 4000);
				        $('#dataGrid').trigger("reloadGrid");
			    	}else {
			    		js.showMessage(e.message, '', 'warning', 4000);
			    	}
			    },"json");
			});
		}
	}else{
		 js.showMessage('请选择一条记录。', '', 'warning', 4000);
		 return;
	}
}
</script>
	<style>
		.laydate-btns-clear{
			display: none !important;
		}
	</style>